﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class CausaDesvioEdit : BasePage
    {
        public CausaDesvio causaDesvioCurrent
        {
            get
            {
                return (CausaDesvio)ViewState["CausaDesvio"];
            }
            set
            {
                ViewState["CausaDesvio"] = value;
            }
        }


        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton guardarButton = new LinkButton();
        LinkButton cancelarButton = new LinkButton();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = "Guardar";
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            cancelarButton.ID = "BoCommandCancelar";
            cancelarButton.Text = "Cancelar";
            cancelarButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(cancelarButton);

            base.LoadContextActions(actionButtons);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(PlanProdSecurity.CAUSADESVIO_VER);

                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        causaDesvioCurrent = new CausaDesvio();
                    }
                    else
                    {
                        causaDesvioCurrent = WebServiceHelper.GetCausasDeDesvioById(Convert.ToInt32(this.Request.QueryString["Id"]));
                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();
            guardarButton.Visible =
            this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            if (causaDesvioCurrent.Id != 0)
            {
                this.txtId.Text = Convert.ToString(causaDesvioCurrent.Id);

                this.txtNombre.Text = causaDesvioCurrent.Nombre;

                BindAuditory(causaDesvioCurrent);
            }
        }

        private void SetData()
        {
            causaDesvioCurrent.EmpresaId = BaseWebPage.CompanyId;
            causaDesvioCurrent.Nombre = this.txtNombre.Text; ;
        }

        private void Save()
        {
            causaDesvioCurrent = WebServiceHelper.SaveCausaDesvio(causaDesvioCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect("/App/CausaDesvioList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("/App/CausaDesvioList.aspx", true);
        }
    }
}